/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Agencia.java
 *
 * Created on 22/05/2013, 17:07:40
 */
package Telas;

import OperacoesArquivo.Leitura;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pkginterface.Agencia;

/**
 *
 * @author jakson
 */
public class TelaAgencia extends javax.swing.JFrame {

    private DefaultTableModel modeloTabela;

    /** Creates new form Agencia */
    public TelaAgencia() {

        initComponents();
        this.setTitle("Agências");
        this.setLocationRelativeTo(this);
        this.modeloTabela = new DefaultTableModel(new Object[]{"Codigo", "Cidade", "UF"}, 0) { // Habilita a não edição das linhas da tabela

            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        };

        this.tabela.setModel(this.modeloTabela);
        this.tabela.getColumnModel().getColumn(0).setMaxWidth(200);
        this.tabela.getColumnModel().getColumn(1).setMaxWidth(200);
        this.tabela.getColumnModel().getColumn(2).setMaxWidth(200);//Define a largura padrão da coluna da tabela               
        this.tabela.setAutoResizeMode(1);
        this.tabela.setSelectionMode(0);
        this.tabela.setCellEditor(null);
        this.tabela.getTableHeader().setSize(200, 10);

        Leitura abre = new Leitura();
        ArrayList<Agencia> ag = new ArrayList<>();
        ag = abre.retornaAgencias();

        for (int i = 0; i < ag.size(); i++) {

            Vector codigo = new Vector();
            Vector cidade = new Vector();
            Vector uf = new Vector();

            codigo.add(ag.get(i).getCodigo());
            cidade.add(ag.get(i).getCidade());
            uf.add(ag.get(i).getUf());

            this.modeloTabela.addRow(new Object[]{codigo.get(0), cidade.get(0), uf.get(0)});


        }



    }

    public void atualiza() {
        this.revalidate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        excluir = new javax.swing.JButton();
        fechar = new javax.swing.JButton();
        editar = new javax.swing.JButton();
        nova = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agências");

        painel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Código", "Cidade", "UF"
            }
        ));
        jScrollPane1.setViewportView(tabela);

        excluir.setText("Excluir");
        excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirActionPerformed(evt);
            }
        });

        fechar.setText("Fechar");
        fechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharActionPerformed(evt);
            }
        });

        editar.setText("Editar");
        editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarActionPerformed(evt);
            }
        });

        nova.setText("Nova");
        nova.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novaActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel1.setText("Tabela de Agências");

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(nova)
                        .addGap(18, 18, 18)
                        .addComponent(editar)
                        .addGap(18, 18, 18)
                        .addComponent(excluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fechar))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fechar)
                    .addComponent(excluir)
                    .addComponent(editar)
                    .addComponent(nova))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void fecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharActionPerformed
// botão fechar esta sendo acionado
    this.dispose();
}//GEN-LAST:event_fecharActionPerformed

private void novaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novaActionPerformed
// botão novo acionado
    Telas.AddAgencia Ag = new Telas.AddAgencia();
    Ag.setLocationRelativeTo(this);
    Ag.setVisible(true);
}//GEN-LAST:event_novaActionPerformed

private void excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirActionPerformed
// botão excluir foi selecionado

    OperacoesArquivo.Exclusao pega = new OperacoesArquivo.Exclusao();
    Leitura ler = new Leitura();
    ArrayList<Agencia> agen = new ArrayList<>();
    agen = ler.retornaAgencias();

    if (this.tabela.getSelectedRow() < 0) {
        JOptionPane.showMessageDialog(null, "selecione uma linha para excluir", "ERRo", WIDTH);
    } else {
        if (JOptionPane.showConfirmDialog(null, "Tem certeza que realmente deseja excluir a Agência", "Sistema Bancário", 0, 3) == 0) {
            pega.excluir(this.tabela.getValueAt(this.tabela.getSelectedRow(), 0).toString(), agen);
            this.modeloTabela.removeRow(this.tabela.getSelectedRow());
        }
    }

    atualiza();


}//GEN-LAST:event_excluirActionPerformed

private void editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarActionPerformed
        
    OperacoesArquivo.Editar pegar = new OperacoesArquivo.Editar();
    Leitura ler = new Leitura();
    ArrayList<Agencia> agen = new ArrayList<>();
    agen = ler.retornaAgencias();

    if (this.tabela.getSelectedRow() < 0) {
        JOptionPane.showMessageDialog(null, "selecione uma linha para editar", "ERRo", WIDTH);
    } else {
        if (JOptionPane.showConfirmDialog(null, "Tem certeza que realmente deseja excluir a Agência", "Sistema Bancário", 0, 3) == 0) {
            pegar.editar(this.tabela.getValueAt(this.tabela.getSelectedRow(), 0).toString(), agen);
            //this.modeloTabela.removeRow(this.tabela.getSelectedRow());
        }
    }   
}//GEN-LAST:event_editarActionPerformed

    /*
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaAgencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaAgencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaAgencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaAgencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaAgencia().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton editar;
    private javax.swing.JButton excluir;
    private javax.swing.JButton fechar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nova;
    private javax.swing.JPanel painel;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables
}
